package com.mypro.mydemo1.demo;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class TestExcel1 {
    public static void main(String[] args) {
        try {

            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("C:\\Users\\WangZhicheng\\Desktop\\授权码.xlsx"));

            int sheetNum = xssfWorkbook.getNumberOfSheets();
            System.out.println("该excel文件中总共有：" + sheetNum + "个sheet");

            for (int i = 0; i < sheetNum; i++) {

                System.out.println("读取第" + (i + 1) + "个sheet");
                XSSFSheet sheet = xssfWorkbook.getSheetAt(i);

                int maxRow = sheet.getLastRowNum();
                for (int row = 0; row <= maxRow; row++) {

                    if (sheet.getRow(row) != null) {
                        int maxRol = sheet.getRow(row).getLastCellNum();
                        System.out.println("--------第" + row + "行的数据如下--------");
                        for (int rol = 0; rol < maxRol; rol++) {
                            System.out.print(sheet.getRow(row).getCell(rol) + "  ");
                        }
                        System.out.println();
                    } else {
                        System.out.println("Row " + row + " does not exist in the sheet.");
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}